﻿using System.Security.Claims;
using BilbilBLL;
using Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Modles.DTO;

namespace bilbil.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    /*-------------------------*/
    //视频页功能
    /*-------------------------*/
    public class VideoPageFunctionController : ControllerBase
    {
        VideoPageFunctionService service = new VideoPageFunctionService();
        /// <summary>
        /// 通过前端传过来的用户信息表id和视频信息表id给收藏表添加数据
        /// </summary>
        [HttpPost]
        public IActionResult AddtoCollection([FromBody] AddCollection_DTO mod)
        {
            Response_DTO<string> res = service.AddtoCollection(mod);
            return new JsonResult(res);
        }
        /// <summary>
        /// 添加视频评论
        /// </summary>
        /// <param name="model">评论信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public IActionResult AddComment([FromBody] AddComment_DTO model)
        {
            try
            {
                var result = service.AddVideoComment(model);
                if (result.Success)
                {
                    return Ok(new
                    {
                        success = true,
                        message = result.Message,
                        data = result.Conten
                    });
                }
                else
                {
                    return BadRequest(new
                    {
                        success = false,
                        message = result.Message,
                        data = result.Conten
                    });
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    success = false,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }
        /// <summary>
        /// 通过前端传过来的用户信息表id和视频信息表id给视频转发表添加数据
        /// </summary>
        /// <param name="mod_DTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddVideoForward([FromBody] VideoForward_DTO mod_DTO)
        {
           ResponseTool res = service.AddVideoForward(mod_DTO);
            return new JsonResult(res);
        }
        /// <summary>
        /// 根据视频ID获取评论数据
        /// </summary>
        /// <param name="videoInfoId">视频信息表ID</param>
        /// <returns>评论数据列表</returns>
        [HttpGet]
        public IActionResult GetCommentsByVideoId(int videoInfoId)
        {
            try
            {
                var result = service.GetCommentsByVideoId(videoInfoId);

                if (result.Success)
                {
                    return Ok(new
                    {
                        success = true,
                        message = result.Message,
                        data = result.Conten
                    });
                }
                else
                {
                    return BadRequest(new
                    {
                        success = false,
                        message = result.Message
                    });
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    success = false,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }


        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public IActionResult AddVideoLike(int videoInfoId)
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            return new JsonResult(service.AddVideoLike(userInfoId, videoInfoId));
        }
        /// <summary>
        /// 取赞
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        public IActionResult DeleteVideoLike(int videoInfoId)
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            return new JsonResult(service.DeleteVideoLike(userInfoId, videoInfoId));
        }


        /// <summary>
        /// 根据视频id获取视频信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public IActionResult GetVideoInfoById(int videoInfoId)
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            return new JsonResult(new VideoPageFunctionService().GetVideoInfoById(userInfoId, videoInfoId));
        }





        /// <summary>
        /// 获取视频点赞收藏转发量
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public IActionResult GetVideoThreeClick(int videoInfoId)
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            return new JsonResult(new VideoPageFunctionService().GetVideoThreeClick(userInfoId, videoInfoId));
        }




        /// <summary>
        /// 添加收藏
        /// </summary>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public IActionResult AddCollectionInfo(int videoInfoId)
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            return new JsonResult(new VideoPageFunctionService().AddCollectionInfo(userInfoId, videoInfoId));

        }
        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        public IActionResult DeleteCollectionInfo(int videoInfoId)
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            return new JsonResult(new VideoPageFunctionService().DeleteCollectionInfo(userInfoId, videoInfoId));
        }


        /// <summary>
        /// 获取视频评论列表
        /// </summary>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVideoComments(int videoInfoId)
        {
            return new JsonResult(new VideoPageFunctionService().GetVideoComments(videoInfoId));
        }


        /// <summary>
        /// 发布视频评论
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public IActionResult AddVideoComment(int videoInfoId, string content)
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            return new JsonResult(new VideoPageFunctionService().AddVideoComment(userInfoId, videoInfoId, content));
        }
    }
}
